/*
 * Main.fx
 *
 * Created on 2010-05-30, 01:09:39
 */

package javafxapplication2;

import javafx.ext.swing.SwingButton;
import javafx.lang.FX;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;
import javafx.stage.Stage;
import java.lang.Runnable;
import java.lang.Void;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

def pool : ExecutorService = Executors.newCachedThreadPool();

public class MultiThreading {
    public var str: String = "string";
}

function process(multi: MultiThreading) {
    var s = multi.str;
    var task: Runnable = Runnable {
        override function run() {
            s = "{s}->changed by a Thread";
            FX.deferAction(function(): Void { multi.str = s });
        }
    }
    pool.execute(task);
}

function run() {
    var multi = MultiThreading{};
    Stage {
        title : "MyApp"
        scene: Scene {
            width: 200
            height: 200
            content: VBox {
                spacing: 20
                content: [
                    SwingButton {
                        text: "Button"
                        action: function() { process(multi) }
                    }
                    Text {
                        wrappingWidth: 200
                        font : Font {
                            size: 16
                        }
                        textOrigin: TextOrigin.TOP
                        content: bind multi.str
                    }
                ]
            }
        }
    }
}
